import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-29
 * Time: 20:36
 */
public class demo10{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的目录：");
        String srcPath = scanner.next();
        File rootFile = new File(srcPath);
        if(!rootFile.isDirectory()) {
            System.out.println("输入的目录不合法");
        }
        System.out.println("请输入要包含的指定内容：");
        String keywords = scanner.next();
        scanDir(rootFile,keywords);
    }

    private static void scanDir(File rootFile,String keywords) {
        File[] files = rootFile.listFiles();
        for (File file : files) {
            if(file.isFile()) {
                printFile(file,keywords);
            }else {
                scanDir(file,keywords);
            }
        }
    }

    private static void printFile(File file, String keywords) {
        if(file.getName().contains(keywords)) {
            System.out.println("名称包含指定字符：" + file.getAbsolutePath());
            return;
        }
        StringBuilder sbd = new StringBuilder();
        try (Reader reader = new FileReader(file)) {
            while(true) {
                char[] chars = new char[1024];
                int n = reader.read(chars);
                if (n == -1) {
                    break;
                }
                sbd.append(chars,0,n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(sbd.indexOf(keywords) > 0) {
            System.out.println("文件内容包含指定字符：" + file.getAbsolutePath());
        }
    }
}
